var Main = Ext.extend(Ext.Viewport, {
	layout : 'border',
	initComponent : function() {
		this.initHeader();
		this.initNav();
		this.initCont();
		this.initFooter();
		this.items = [this.header, this.nav, this.cont, this.footer];
		Main.superclass.initComponent.call(this);
	},
	initHeader : function() {
		var logo = new Ext.Container({
					border : false,
					flex : 1,
					layout : 'hbox',
					items : [{
								xtype : 'container',
								autoEl : {
									tag : 'img',
									src : My.url('/resources/images/stats.png'),
									style : 'vertical-align:middle;margin:4px;'
								}
							}, {
								xtype : 'container',
								style : 'line-height:40px;margin-left:10px;',
								html : '进销存管理系统'
							}]
				});
		this.header = new Ext.Panel({
					region : 'north',
					height : 42,
					layout : {
						type : 'hbox',
						pack : 'start',
						align : 'stretch'
					},
					bodyStyle : 'background:transparent;',
					// bodyStyle : 'background:url('
					// + My.url('/resources/images/hd-bg.png')
					// + ') repeat-x;',
					items : [logo, {
						xtype : 'container',
						layout : 'hbox',
						layoutConfig : {
							pack : 'end',
							align : 'middle',
							padding : '0px 2px 0px 2px'
						},
						width : 100,
						items : [{
							xtype : 'button',
							text : '退出系统',
							iconCls : 'icon-large-logout',
							scale : 'large',
							iconAlign : 'left',
							handler : function() {
								My.confirm('确定要退出系统吗?', function(btn) {
											if (btn == 'yes') {
												document.location = My
														.url('/logout.htm');
											}
										});
							}
						}]
					}]
				});
	},
	initNav : function() {
		var me = this;
		this.nav = new Ext.TabPanel({
			activeItem : 0,
			region : 'west',
			width : 200,
			split : true,
			bodyStyle : 'border-bottom:0;',
			headerStyle : 'border-top:0;',
			items : [{
				xtype : 'treepanel',
				title : '系统菜单',
				layout : 'fit',
				autoScroll : true,
				useArrows : true,
				rootVisible : false,
				root : {
					id : '0',
					text : '系统菜单',
					expanded : true
				},
				loader : new Ext.tree.TreeLoader({
							nodeParameter : 'parentid',
							baseParams : {
								userid : USER.userid
										|| '50fbc1d1-4760-455e-b018-dff07a4cb672'
							},
							dataUrl : My
									.url('/system/permis/menu/getMenuListByUser.do'),
							createNode : function(attr) {
								attr.id = attr.id || attr.menuid;
								attr.text = attr.text || attr.menuname;
								if (attr.ico) {
									attr.iconCls = '" style="background-image: url('
											+ My.url('/resources/images/menu/'
													+ attr.ico) + ');';
								}
								return Ext.tree.TreeLoader.prototype.createNode
										.call(this, attr);
							}
						}),
				listeners : {
					click : function(node) {
						var attr = node.attributes;
						if (!attr.url) {
							return;
						}
						me.addCard({
									itemId : attr.id,
									title : attr.text,
									src : attr.url,
									ico : attr.ico
								});
					}
				}
			}]
		});
	},
	initCont : function() {
		this.cont = new Ext.TabPanel({
			region : 'center',
			activeItem : 0,
			bodyStyle : 'border-bottom:0;',
			headerStyle : 'border-top:0;',
			plugins : [new Ext.ux.TabCloseMenu({
								closeTabText : '关闭当前选项卡',
								closeOtherTabsText : '关闭其它选项卡',
								closeAllTabsText : '关闭所有选项卡'
							}), new Ext.ux.TabScrollerMenu()],
			items : [{
				title : '<table cellpadding="0" cellspacing="0" border="0"><tr><td width="18"><img src="'
						+ My.url('/resources/images/menu/home.png')
						+ '" /></td><td>首页</td></tr></table>'
			}]
		});
	},
	initFooter : function() {
		this.footer = new Ext.Panel({
			border : false,
			height : 27,
			region : 'south',
			bbar : new Ext.Toolbar({
				height : 27,
				items : ['当前用户:', USER.name, '->', {
					listeners : {
						render : function() {
							var clock = this;
							var date = new Date();
							Ext.TaskMgr.start({
										run : function() {
											date.setTime(date.getTime() + 1000);
											var ti = date.format('Y-m-d H:i:s');
											clock.setText(ti);
										},
										interval : 1000
									});
						}
					}
				}]
			})
		});
	},
	getCard : function(itemId) {
		return this.cont.getComponent(itemId);
	},
	addCard : function(card) {
		if (card instanceof Ext.Panel || card instanceof Ext.FormPanel) {
			var itemId = card.getItemId();
			if (!this.getCard(itemId)) {
				this.cont.add(card);
			}
		} else {
			var attr = Ext.apply({
						itemId : Ext.id(),
						src : 'about:blank'
					}, card);
			card = this.getCard(attr.itemId);
			if (!card) {
				var title = '<table cellpadding="0" cellspacing="0" border="0"><tr><td width="18"><img src="'
						+ My
								.url('/common/extjs/resources/images/default/tree/leaf.gif')
						+ '" /></td><td>' + attr.title + '</td></tr></table>';
				if (attr.ico) {
					title = '<table cellpadding="0" cellspacing="0" border="0"><tr><td width="18"><img width="16" height="16" src="'
							+ My.url('/resources/images/menu/' + attr.ico)
							+ '" /></td><td>'
							+ attr.title
							+ '</td></tr></table>';
				}
				card = new Ext.Panel({
							closable : true,
							title : title,
							itemId : attr.itemId,
							layout : 'fit',
							items : [{
										xtype : 'container',
										autoEl : {
											tag : 'iframe',
											name : 'iframe_' + Ext.id(),
											src : My.url(attr.src),
											frameborder : 0
										}
									}],
							refresh : function() {
								var iframe = this.items.itemAt(0);
								iframe.getEl().dom.contentWindow.location
										.reload();
							}
						});
				this.cont.add(card);
			} else {
				// 刷新
				if (Ext.isFunction(card.refresh)) {
					card.refresh();
				}
			}
		}
		this.cont.setActiveTab(card);
	}
});
Ext.onReady(function() {
			new Main();
		});